package com.two.service.shopcar.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.two.common.util.CommonResult;
import com.two.service.shopcar.entity.Shopcar;
import com.two.service.shopcar.mapper.ShopcarMapper;
import com.two.service.shopcar.service.IShopcarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.two.service.shopcar.vo.RestautantVo;
import com.two.service.shopcar.vo.ShopCarVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪瑞明
 * @since 2022-09-01
 */
@Service
public class ShopcarServiceImpl extends ServiceImpl<ShopcarMapper, Shopcar> implements IShopcarService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ShopcarMapper shopcarMapper;

    // 添加商品
    @Override
   /* public CommonResult addGoods(String token, ShopCarVo shopCarVo) {
        // 根据token获取username
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String usermonicker = user.getString("usermonicker");

        shopCarVo.setUsermonicker(usermonicker);

        QueryWrapper<Shopcar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_name",shopCarVo.getDishName());
        queryWrapper.eq("username",shopCarVo.getUsermonicker());
        queryWrapper.eq("rid",shopCarVo.getRid());
        Shopcar shopcar1 = shopcarMapper.selectOne(queryWrapper);
        // 判断该用户在该店铺中是否添加过该商品
        if (shopcar1!=null){
            System.out.println("===============");
            //获取再次添加的数量
            Integer dishNum = shopCarVo.getDishNum();
            // 获取之前商品的数量
            Integer dishNum1 = shopcar1.getDishNum();
            Integer num=dishNum1+dishNum;
            System.out.println("1111=====>"+num);
            // 修改本次订单的数量
            shopcar1.setDishNum(num);

            // 修改数据库中的商品数量
            int x = shopcarMapper.update(shopcar1, queryWrapper);
            //int x = shopcarMapper.updateNumber(s);
            if (x==1){
                return new CommonResult(2000,"添加成功",shopCarVo);
            }
            return new CommonResult(5000,"添加失败",null);
        }

        Integer i = shopcarMapper.insertGoods(shopCarVo);
        if (i!=0){
            return new CommonResult(2000,"添加成功",null);
        }

        return new CommonResult(5000,"添加失败",null);
    }*/
    public CommonResult addGoods(String token, Shopcar[] shopcar) {
        // 根据token获取username
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String usermonicker = user.getString("usermonicker");

        shopcar[0].setUsermonicker(usermonicker);

        QueryWrapper<Shopcar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_name",shopcar[0].getDishName());
        queryWrapper.eq("usermonicker",shopcar[0].getUsermonicker());
        queryWrapper.eq("rid",shopcar[0].getRid());
        Shopcar shopcar1 = shopcarMapper.selectOne(queryWrapper);
        // 判断该用户在该店铺中是否添加过该商品
        if (shopcar1!=null){

            //获取再次添加的数量
            Integer dishNum = shopcar[0].getDishNum();
            System.out.println("==============="+dishNum);
            // 如果添加的数量为0 则删除购物车中该商品
            if (dishNum==0){
                String id = shopcar1.getId();
                int i = shopcarMapper.deleteGoods(id);
                if (i==1){
                    return new CommonResult(2000,"添加成功",shopcar);
                }
                return new CommonResult(5000,"添加失败",null);
            }

            // 修改本次订单的数量
            shopcar[0].setDishNum(dishNum);

            // 修改数据库中的商品数量
            int x = shopcarMapper.update(shopcar[0], queryWrapper);
            if (x==1){
                return new CommonResult(2000,"添加成功",shopcar);
            }
            return new CommonResult(5000,"添加失败",null);
        }



        Integer i = shopcarMapper.insert(shopcar[0]);
        if (i!=0){
            return new CommonResult(2000,"添加成功",null);
        }

        return new CommonResult(5000,"添加失败",null);
    }
    // 查询购物车
    @Override
    public CommonResult findAll( String token) {
        // 根据token获取username
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String rid = user.getString("rid");
        String usermonicker = user.getString("usermonicker");
        // 获取购物车中餐厅的信息
        List <RestautantVo> restautantVo=shopcarMapper.selRestaurant(rid,usermonicker);
        System.out.println("restautantVo=======>"+restautantVo.toString());

        // 查询购物车中商品的信息
        QueryWrapper<Shopcar> wrapper = new QueryWrapper<>();
        wrapper.eq("usermonicker",usermonicker);
        List<Shopcar> shopcars = shopcarMapper.selectList(wrapper);
        //List<ShopCarVo> shopcars = shopcarMapper.findAll(usermonicker);
        // 将菜品放入对应的商店
        for (int i = 0; i < restautantVo.size(); i++) {
            List <Shopcar> shopcar=new ArrayList<>();
            for (int j = 0; j < shopcars.size(); j++) {

                if (restautantVo.get(i).getRid().equals(shopcars.get(j).getRid())){

                    shopcar.add(shopcars.get(j));
                }
            }
            restautantVo.get(i).setShopcar(shopcar);
        }
        System.out.println("restautantVo=====>"+restautantVo);
        // Page<Shopcar> page = new Page<>(currentPage, pageSize);
       // shopcarMapper.selectPage(page,wrapper);
        // List<Shopcar> list = shopcarMapper.selectList(wrapper);


        return new CommonResult(2000,"查询成功",restautantVo);
    }

    // 根据ID删除商品
    @Override
    public CommonResult deleteById(String id) {

        int i = shopcarMapper.deleteById(id);
        //int i = shopcarMapper.deleteById(id);

        if (i!=0){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);

    }

    // 手机端批量删除
    @Override
    public CommonResult deleteBySelect(RestautantVo[] restautantVo) {
        int x = 0;
        for (int i = 0; i < restautantVo.length; i++) {
            for (int j = 0; j < restautantVo[i].getShopcar().size(); j++) {
                x= shopcarMapper.deleteGoods(restautantVo[i].getShopcar().get(j).getId());
            }
        }
       /* int length = shopcar.length;
        if (length<1){
            return new CommonResult(5000,"删除商品不能为空",null);
        }
        for (int i = 0; i < length; i++) {
            shopcarMapper.deleteById(shopcar[i]);
        }*/
        if (x!=0){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败，请稍后重试",null);
    }

    //


    // 查询菜单中商品数量
    @Override
    public CommonResult findGoodsNum(String token) {
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String usermonicker = user.getString("usermonicker");
        List list = shopcarMapper.findGoodsNum(usermonicker);
        System.out.println("usermonicker======>"+list);
        return new CommonResult(2000,"查询数量成功",list);
    }

    @Override
    public void deleteGoods(String id) {
         shopcarMapper.deleteGoods(id);
    }

}
